/*
 * @Author: huangtianyang 916072572@qq.com
 * @Date: 2022-12-15 16:29:55
 * @LastEditors: huangtianyang
 * @LastEditTime: 2023-08-31 15:11:11
 * @FilePath: \cxl-admin\src\pages\finance\refund\operateDialog\index.ts
 */
import { getCompanyBalanceById, refundOptions } from '@/apis/finance/pay'
import { refundDetailed } from '@/apis/finance/refund'
import { regBankAccount } from '@/utils'
import { bankMap } from '@/utils/bank'
import axios from 'axios'
import { reactive } from 'vue'

export class OperateDialog {
    //公司id
    companyId = ''
    //订单信息
    orderInformation = reactive({
        companyName: '', //公司名
        phone: '', //电话
        totalStoreAmount: '', //总充值金额
        totalFreeCount: '', //总赠送次数
        remainFreeCount: '', //剩余赠送次数
        usedFreeCount: '', //使用次数
        refundReason: '', //退款原因
        optUser: '', //操作人
        optTime: '', //操作时间
        bankAccount: '', //银行卡号
        branches: '', //开户网点
        brank: '', //开户行
        certificate: '', //退款凭证
        description: '', //描述
        receiverName: '', //收款人姓名
        refundAmount: 0, //退款金额
        storeRefundId: '', //退款id
        id: '',
        companyId: ''
    })
    refundData = reactive({
        cause: '', //退款原因
        remark: '', //退款备注
        bank: '', //收款银行
        branch: '', //开户网点
        card: '', //银行卡号
        name: '', //收款姓名
        refundMoney: '' //退款金额
    })
    rules = {
        cause: { required: false, message: '请选择退款原因', trigger: 'change' },
        bank: { required: false, message: '请输入收款银行', trigger: 'blur' },
        branch: { required: false, message: '请输入开户网点', trigger: 'blur' },
        card: { required: false, validator: this.cardValidatePass.bind(this), trigger: 'blur' },
        name: { required: false, message: '请输入收款姓名', trigger: 'blur' }
    }

    constructor(companyId: string) {
        this.companyId = companyId
    }

    cardValidatePass(rule: any, value: any, callback: any) {
        if (value === '') {
            callback(new Error('银行卡号有误，请重新输入'))
        } else {
            if (regBankAccount(value)) {
                callback()
                //验证通过，通过卡号查询所属银行
                axios({
                    method: 'get',
                    url: `https://ccdcapi.alipay.com/validateAndCacheCardInfo.json?_input_charset=utf-8&cardNo=${value}&cardBinCheck=true`
                }).then(res => {
                    //根据返回的bank与map中的数据进行匹配
                    this.refundData.bank = bankMap.get(res.data.bank)
                })
            } else {
                this.refundData.bank = ''
                callback(new Error('银行卡号有误，请重新输入'))
            }
        }
    }
    /**
     * 获取公司钱包
     */
    getCompanyBalance() {
        getCompanyBalanceById(this.companyId)
    }

    getRefundDetailedById = (data: any) => {
        if (data.id == '') {
            this.orderInformation.companyName = data.companyName
            this.orderInformation.phone = data.phone
            this.orderInformation.totalStoreAmount = data.remainAmount
            this.orderInformation.totalFreeCount = data.remainFreeCount
            this.orderInformation.usedFreeCount = data.totalSearchCount
            this.orderInformation.id = data.id
            this.orderInformation.companyId = data.companyId
        } else {
            refundDetailed(data.id).then(res => {
                if (res.data.value === 200) {
                    this.orderInformation.totalStoreAmount = res.data.data.totalStoreAmount
                    this.orderInformation.totalFreeCount = res.data.data.totalFreeCount
                    this.orderInformation.remainFreeCount = res.data.data.remainFreeCount
                    this.orderInformation.usedFreeCount = res.data.data.usedFreeCount
                    this.orderInformation.refundReason = res.data.data.refundReason
                    this.orderInformation.bankAccount = res.data.data.bankAccount
                    this.orderInformation.branches = res.data.data.branches
                    this.orderInformation.brank = res.data.data.brank
                    this.orderInformation.certificate = res.data.data.certificate
                    this.orderInformation.description = res.data.data.description
                    this.orderInformation.receiverName = res.data.data.receiverName
                    this.orderInformation.refundAmount = res.data.data.refundAmount

                    //
                    this.orderInformation.companyName = data.companyName
                    this.orderInformation.phone = data.phone
                    this.orderInformation.optUser = data.optUser
                    this.orderInformation.optTime = data.optTime
                    this.orderInformation.storeRefundId = data.storeRefundId
                    this.orderInformation.id = data.id
                    this.orderInformation.companyId = data.companyId

                    //退款数据
                    this.refundData.cause = res.data.data.refundReason
                        ? refundOptions[res.data.data.refundReason - 1].label
                        : ''
                    this.refundData.remark = res.data.data.description
                    this.refundData.bank = res.data.data.brank
                    this.refundData.branch = res.data.data.branches
                    this.refundData.card = res.data.data.bankAccount
                    this.refundData.name = res.data.data.receiverName
                    this.refundData.refundMoney = res.data.data.refundAmount
                }
            })
        }
    }
}
